//
//  HSaveTextViewController.m
//  逆向工程
//
//  Created by 9188 on 2017/5/17.
//  Copyright © 2017年 朱同海. All rights reserved.
//

#import "HSaveTextViewController.h"
#import "HCustomServiceView.h"
#import "HCustomServiceView.m"

#import "HCustomNumberView.h"
#import "HCustomNumberView.m"

#import "UIView+SCYCategory.h"
#import "UIView+SCYCategory.m"

#import "CDAutoHideMessageHUD.h"
#import "CDAutoHideMessageHUD.m"

#import "HUserRelevantUtilities.h"
#import "HUserRelevantUtilities.m"

#import <AdSupport/AdSupport.h>

#import "src/Request.h"
#import "src/Request.m"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define HKeyWindow ([UIApplication sharedApplication].keyWindow )
#define COLOR(r,g,b,a) ([UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)])
#define SHORTCOLOR(r) ([UIColor colorWithRed:(r)/255.0f green:(r)/255.0f blue:(r)/255.0f alpha:(1)])
#define kColor_Background       SHORTCOLOR(0xf5)
#define kColor_Line             SHORTCOLOR(0xdd)

@interface HSaveTextViewController ()<UITextViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) HCustomServiceView *customView_0;
@property (nonatomic, strong) HCustomServiceView *customView_1;
@property (nonatomic, strong) HCustomServiceView *customView_2;

@property (nonatomic, strong) UIButton *saveBtn;
@property (nonatomic, strong) UIButton *nextViewBtn;

@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UILabel *hongZhaLabel;
@property (nonatomic, strong) UISwitch *hongZhaSwitch;

@property (nonatomic, strong) UILabel *labNum1;
@property (nonatomic, strong) UILabel *labNum2;
@property (nonatomic, strong) UILabel *labNum3;

@property (nonatomic, strong) UILabel *hongZhaNum;
@property (nonatomic, strong) UILabel *hongZhaTime;

@property (nonatomic, strong) UILabel *customLabel;
@property (nonatomic, strong) UILabel *attentionLabel;
@property (nonatomic, strong) UILabel *versionLab;


@property (nonatomic, strong) UITextField *countTextFeild;
@property (nonatomic, strong) UITextField *timeTextFeild;

@property (nonatomic, strong) UISlider *slider1; //轰炸间隔
@property (nonatomic, strong) UISlider *slider2; //轰炸次数
@property (nonatomic, strong) UISlider *slider3; //自定义编号

@property (nonatomic, strong) UIView *line1;
@property (nonatomic, strong) UIView *line2;

@property (nonatomic, strong) UIView *bgView1;
@property (nonatomic, strong) UIView *bgView2;
@property (nonatomic, strong) UIView *bgView3;

@property (nonatomic, strong) HCustomNumberView *customNumberView;

@property (nonatomic, assign) NSString *numberText;

@property (nonatomic, strong) NSMutableArray *customNumberArray;
@property (nonatomic, strong) NSMutableArray *customServiceArray;

//@property (nonatomic, strong) NSURLSessionDataTask *sessionDataTask;
@property (nonatomic, assign) NSInteger loadTimes;

@end

void showAlertViewWithTitleAndMessage(NSString *title, NSString *message) {
    if (!message || message.length==0) { return; }
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:title
                                                     message:message
                                                    delegate:nil
                                           cancelButtonTitle:@"确定"
                                           otherButtonTitles:nil, nil];
    [alertView show];
}

void showAlertViewWithMessage(NSString *message) {
    showAlertViewWithTitleAndMessage(nil, message);
}

@implementation HSaveTextViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.loadTimes = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"诛天";
    [self createUI];
    [self createLeftBarButton];
}

#pragma mark - create UI

- (void)createUI {

    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.contentSize = CGSizeMake(0, 550);
    [self.view addSubview:self.scrollView];

    // [self.scrollView addSubview:self.saveBtn];
    // [self.scrollView addSubview:self.nextViewBtn];
    
    // [self.scrollView addSubview:self.textView];
    // [self.scrollView addSubview:self.contentLabel];

    // [self.scrollView addSubview:self.hongZhaLabel];
    // [self.scrollView addSubview:self.hongZhaSwitch];

    // [self.scrollView addSubview:self.hongZhaNum];
    // [self.scrollView addSubview:self.hongZhaTime];

    //[self.scrollView addSubview:self.countTextFeild];
    //[self.scrollView addSubview:self.timeTextFeild];

    [self bgView1];
    [self customView_0];
    [self customView_1];
    [self customView_2];

    [self bgView2];
    [self hongZhaLabel];
    [self hongZhaSwitch];
    [self hongZhaTime];
    [self hongZhaNum];
    [self slider1];
    [self slider2];
    [self labNum1];
    [self labNum2];
    [self labNum3];

    [self bgView3];
    // [self customLabel];
    //[self attentionLabel];
    //[self slider3];
    [self contentLabel];
    [self textView];

    [self saveBtn];

   [self versionLab];

}

- (void)createLeftBarButton {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(-20, 0, 60, 30)];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self.navigationItem setLeftBarButtonItem:item];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self hiddenKeyboard];
}

- (void)backAction:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - private

/// 自定义的内容
- (void)p_actionnextBtn {
    NSLog(@"进入自定义页面");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定义" message:@"请输入设置编号" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"请输入1～99之间的数字";

    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *NumbereText = alertController.textFields.firstObject;
        NSString *text = NumbereText.text;
        if ((text.integerValue >0 && text.integerValue<100)) {
            NSLog(@"%@", text);
            self.numberText = text;
            [self dealWithSureActionWithText:text];
        } else {
            NSLog(@"请输入的有误，请重新输入");
            [CDAutoHideMessageHUD showMessage:@"请输入1-99之间的数字"];
            [self p_actionnextBtn];
        }
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - request data

- (void)p_actionSaveBtn {

    if ([_hongZhaSwitch isOn]){
        self.labNum2.text = @"9999";
        if (self.labNum1.text.length == 0) {
            return [CDAutoHideMessageHUD showMessage:@"请设置轰炸间隔"];
        }
    } else {
        if (self.labNum2.text.length == 0) {
            return [CDAutoHideMessageHUD showMessage:@"请设置轰炸次数"];
        }

        if (self.labNum1.text.length == 0) {
            return [CDAutoHideMessageHUD showMessage:@"请设置轰炸间隔"];
        }
    }

    HSaveBombCount([self labNum2].text);
    HSaveTimeInterval([self labNum1].text);

    if (self.loadTimes > 0) {
        [CDAutoHideMessageHUD showMessage:@"您已经获取过服务器，请退出微信重新获取"];
        return ;
    }
    if (self.customView_0.on == NO && self.customView_1.on == NO && self.customView_2.on == NO) {
        [CDAutoHideMessageHUD showMessage:@"请打开服务器内容开关"];
        return;
    }

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"uid"] = HEmpowerString();
    params[@"deviceStr"] = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSString *path = @"http://hb.mkejt.com/message/toSendInfo.do";
    NSDictionary *dict = [[Request sharedInstance] apiPost:path param:params];
    NSString *msg = dict[@"info"];
        if (msg.length <= 0) {
            showAlertViewWithMessage(@"操作失败，请重新设置!");
        } else {
            NSLog(@"开始保存数据");
            HClearServiceTextArray();
            [self.customServiceArray removeAllObjects];
            NSDictionary *textdict = dict;
             NSArray *array = textdict.allValues;
            __block NSMutableArray *arrayTemp = [NSMutableArray array];

            if (self.textView.text) {
                [array enumerateObjectsUsingBlock:^(NSString *string, NSUInteger idx, BOOL * _Nonnull stop) {
                    [arrayTemp addObject:[NSString stringWithFormat:@"%@%@",self.textView.text, string]];
                }];   
                self.customServiceArray =  [NSMutableArray arrayWithArray:arrayTemp];

            } else {
                self.customServiceArray = [NSMutableArray arrayWithArray:array];
            }
                        
            HSaveServiceTextArray(self.customServiceArray);
            self.loadTimes ++;
            showAlertViewWithMessage(@"恭喜您，选择服务器数据成功");
            NSLog(@"保存数据成功");
        }
}

#pragma mark - private

/// 处理自定义能容
- (void)dealWithSureActionWithText:(NSString *)text {
    [self.customNumberView setTitleLabelText:text];
    [HKeyWindow showView:self.customNumberView backViewAlpha:0.8 target:nil touchAction:nil];
}

- (void)actionBtnCLickOnCustomNumberView:(UIButton *)btn {
    switch (btn.tag) {
        case 1314: // 保存内容
            [self saveCustomNumerText];
            break;
        case 1315: // 清除内容
            [self clearCustomNumberText];
            break;
        case 1316: // 快速返回
            [self quicklyReturnAlertView];
            break;
        case 1317: // 清除缓存
        {
            [self.customNumberArray removeAllObjects];
            HClearCustomNumberArray();
            NSArray *arr = HCustomNumberArray();
            if (!arr || arr.count == 0) {
                showAlertViewWithMessage(@"您已经清除所有自定义编号内容");
            }
        }
            break;
        default:
            break;
    }
}

/// 保存自定义编号内容
- (void)saveCustomNumerText {
    if (self.customNumberView.myTextView.text.length <= 0) {
        [CDAutoHideMessageHUD showMessage:@"请输入定义编号内容"];
    } else {
        [HKeyWindow hideView:self.customNumberView];
        [self saveData];
        self.customNumberView.myTextView.text = nil;
        showAlertViewWithMessage([NSString stringWithFormat:@"保存成功，当前共自定义了%lu条数据", (unsigned long)self.customNumberArray.count]);
    }
}

/// 获取随机数
- (NSString *)getRandomNumber {
    int number = arc4random() % 10000000000;
    return [NSString stringWithFormat:@"%d", number];
}

// 保存数据
- (void)saveData {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setObject: self.customNumberView.myTextView.text forKey:self.numberText];
    NSArray *array = [NSArray arrayWithArray:self.customNumberArray].mutableCopy;
    [array enumerateObjectsUsingBlock:^(NSMutableDictionary  *tempDict, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([tempDict.allKeys.firstObject isEqualToString:dict.allKeys.firstObject]) {
            [self.customNumberArray removeObject:tempDict];
        }
    }];
    [self.customNumberArray addObject:dict];
    NSMutableArray *array1 = [NSMutableArray arrayWithArray:self.customNumberArray].mutableCopy;
    NSMutableArray *arrayTemp = [NSMutableArray array];
    [self.customNumberArray removeAllObjects];
    [array1 enumerateObjectsUsingBlock:^(NSMutableDictionary  *tempDict, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *dict =tempDict.mutableCopy;
        if (self.textView.text == nil) {
            self.textView.text = @"";
        }
        [dict setObject:[NSString stringWithFormat:@"%@%@", self.textView.text, tempDict.allValues.firstObject] forKey:tempDict.allKeys.firstObject];// 先是内容头   后是自定义内容
        [arrayTemp addObject:dict];
    }];
    [self.customNumberArray removeAllObjects];
    self.customNumberArray = [NSMutableArray arrayWithArray:arrayTemp];
    self.customNumberArray = [self sortWith:self.customNumberArray];
    HSaveCustomNumberArray(self.customNumberArray);
}

/// 清除自定义内容
- (void)clearCustomNumberText {
    self.customNumberView.myTextView.text = nil;
}

/// 快速跳转
- (void)quicklyReturnAlertView {
    self.customNumberView.myTextView.text = nil;
    [HKeyWindow hideView:self.customNumberView];
    [self p_actionnextBtn];
}

#pragma mark - lazy

- (UIView *)bgView1
{
    if (!_bgView1)
    {
        _bgView1 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 120)];
        _bgView1.backgroundColor = kColor_Background;
        _bgView1.layer.cornerRadius = 5.0f;
        _bgView1.layer.masksToBounds = YES;
        [self.scrollView addSubview:_bgView1];
    }
    return _bgView1;
}

- (UIView *)bgView2
{
    if (!_bgView2)
    {
        _bgView2 = [[UIView alloc] initWithFrame:CGRectMake(10, 140, SCREEN_WIDTH-20, 120)];
        _bgView2.backgroundColor = kColor_Background;
        _bgView2.layer.cornerRadius = 5.0f;
        _bgView2.layer.masksToBounds = YES;
        [self.scrollView addSubview:_bgView2];
    }
    return _bgView2;
}
- (UIView *)bgView3
{
    if (!_bgView3)
    {
        _bgView3 = [[UIView alloc] initWithFrame:CGRectMake(10, 270, SCREEN_WIDTH-20, 120)];
        _bgView3.backgroundColor = kColor_Background;
        _bgView3.layer.cornerRadius = 5.0f;
        _bgView3.layer.masksToBounds = YES;
        [self.scrollView addSubview:_bgView3];
    }
    return _bgView3;
}

-(HCustomServiceView *)customView_0 {
    if (!_customView_0) {
        _customView_0 = [[HCustomServiceView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, 40)];
        [_customView_0 setTitleLabelText:@"核爆灭群代码1" switchIsOn:NO];
        [self.bgView1 addSubview:_customView_0];
    }
    return _customView_0;
}

-(HCustomServiceView *)customView_1 {
    if (!_customView_1) {
        _customView_1 = [[HCustomServiceView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH-20, 40)];
        [_customView_1 setTitleLabelText:@"核爆灭群代码2" switchIsOn:NO];
        [self.bgView1 addSubview:_customView_1];
    }
    return _customView_1;
}

-(HCustomServiceView *)customView_2 {
    if (!_customView_2) {
        _customView_2 = [[HCustomServiceView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH-20, 40)];
        [_customView_2 setTitleLabelText:@"核爆灭群代码3" switchIsOn:NO];
        _customView_2.line.hidden = YES;
        [self.bgView1 addSubview:_customView_2];
    }
    return _customView_2;
}


- (UIButton *)saveBtn {
    if (!_saveBtn) {
        _saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 430, SCREEN_WIDTH-20, 40)];
        [_saveBtn setTitleColor:[[UIColor blackColor] colorWithAlphaComponent:0.7] forState:UIControlStateNormal];
        [_saveBtn setTitle:@"保存设置" forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_saveBtn addTarget:self action:@selector(p_actionSaveBtn) forControlEvents:UIControlEventTouchUpInside];
        _saveBtn.backgroundColor = kColor_Background;
        _saveBtn.layer.borderWidth = 0.5f;
        _saveBtn.layer.cornerRadius = 5;
        _saveBtn.layer.masksToBounds = YES;
        [self.scrollView addSubview:_saveBtn];
    }
    return _saveBtn;
}


- (UIButton *)nextViewBtn {
    if (!_nextViewBtn) {
        _nextViewBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-40, 30)];
        [_nextViewBtn setTitleColor:[[UIColor blackColor] colorWithAlphaComponent:0.7] forState:UIControlStateNormal];
        [_nextViewBtn setTitle:@"自定义" forState:UIControlStateNormal];
        _nextViewBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_nextViewBtn addTarget:self action:@selector(p_actionnextBtn) forControlEvents:UIControlEventTouchUpInside];
        _nextViewBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _nextViewBtn.layer.borderWidth = 0.5f;
        _nextViewBtn.layer.cornerRadius = 5;
        _nextViewBtn.layer.masksToBounds = YES;
    }
    return _nextViewBtn;
}

- (UILabel *)customLabel {
    if (!_customLabel) {
        _customLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 60, 30)];
        _customLabel.font = [UIFont systemFontOfSize:16];
        _customLabel.textAlignment = NSTextAlignmentLeft;
        _customLabel.text = @"自定义";
        _customLabel.textColor = [UIColor blackColor];
        [_bgView3 addSubview:_customLabel];
    }
    return _contentLabel;
}

- (UILabel *)attentionLabel {
    if (!_attentionLabel) {
        _attentionLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 5, 200, 30)];
        _attentionLabel.font = [UIFont systemFontOfSize:13];
        _attentionLabel.textAlignment = NSTextAlignmentLeft;
        _attentionLabel.text = @"(请选择1-99的编号)";
        _attentionLabel.textColor = [UIColor lightGrayColor];
        [_bgView3 addSubview:_attentionLabel];
    }
    return _attentionLabel;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
        _contentLabel.font = [UIFont systemFontOfSize:16];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.text = @"内容头";
        _contentLabel.textColor = [UIColor blackColor];
        [self.bgView3 addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (UILabel *)versionLab {
    if (!_versionLab) {
        _versionLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 480, SCREEN_WIDTH, 20)];
        _versionLab.font = [UIFont systemFontOfSize:13];
        _versionLab.textAlignment = NSTextAlignmentCenter;
        _versionLab.text = @"v2.0";
        _versionLab.textColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:_versionLab];
    }
    return _versionLab;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.frame = CGRectMake( 10, 40, SCREEN_WIDTH - 40, 70);
        _textView.delegate = self;
        _textView.layer.borderColor = kColor_Line.CGColor;
        _textView.layer.borderWidth = 0.5f;
        _textView.layer.cornerRadius = 3;
        [_bgView3 addSubview:_textView];
    }
    return _textView;
}

- (UILabel *)hongZhaLabel {
    if (!_hongZhaLabel) {
        _hongZhaLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 80, 30)];
        _hongZhaLabel.font = [UIFont systemFontOfSize:16];
        _hongZhaLabel.textAlignment = NSTextAlignmentLeft;
        _hongZhaLabel.text = @"无限轰炸";
        _hongZhaLabel.textColor = [UIColor blackColor];
        [self.bgView2 addSubview:_hongZhaLabel];

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 40, SCREEN_WIDTH-40, 0.5)];
        line.backgroundColor = kColor_Line;
        [_bgView2 addSubview:line];
    }
    return _hongZhaLabel;
}

- (UISwitch *)hongZhaSwitch
{
    if (!_hongZhaSwitch) {
        _hongZhaSwitch = [[UISwitch alloc]init];
        _hongZhaSwitch.frame = CGRectMake(SCREEN_WIDTH-85, 5, 50, 30);
        [_hongZhaSwitch setOn:NO];
        [_hongZhaSwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
        [self.bgView2 addSubview:_hongZhaSwitch];
    }
    return _hongZhaSwitch;
}

-(void)switchIsChanged:(UISwitch *)swith
{
    if ([_hongZhaSwitch isOn]){
        _labNum1.text = @"1";
        _labNum2.text = @"9999";
        _slider1.enabled = NO;
        _slider2.enabled = NO;
        _slider1.value = 1;
        _slider2.value = 100;
    } else {
         _labNum1.text = @"0";
        _labNum2.text = @"0";
        _slider1.enabled = YES;
        _slider2.enabled = YES;
         _slider1.value = 0;
        _slider2.value = 0;
    }
    
}

- (UILabel *)hongZhaTime {
    if (!_hongZhaTime) {
        _hongZhaTime = [[UILabel alloc] initWithFrame:CGRectMake(10, 45, SCREEN_WIDTH-40, 30)];
        _hongZhaTime.font = [UIFont systemFontOfSize:16];
        _hongZhaTime.textAlignment = NSTextAlignmentLeft;
        _hongZhaTime.text = @"轰炸间隔(秒):";
        _hongZhaTime.textColor = [UIColor blackColor];
        [_bgView2 addSubview:_hongZhaTime];

        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 80, SCREEN_WIDTH-40, 0.5)];
        line.backgroundColor = kColor_Line;
        [_bgView2 addSubview:line];
    }
    return _hongZhaTime;
}

- (UILabel *)hongZhaNum {
    if (!_hongZhaNum) {
        _hongZhaNum = [[UILabel alloc] initWithFrame:CGRectMake(10, 85, SCREEN_WIDTH-40, 30)];
        _hongZhaNum.font = [UIFont systemFontOfSize:16];
        _hongZhaNum.textAlignment = NSTextAlignmentLeft;
        _hongZhaNum.text = @"轰炸次数(次):";
        _hongZhaNum.textColor = [UIColor blackColor];
        [_bgView2 addSubview:_hongZhaNum];
    }
    return _hongZhaNum;
}

- (UITextField *)countTextFeild
{
    if(!_countTextFeild){
        _countTextFeild = [[UITextField alloc] initWithFrame:CGRectMake(120, 410, 165, 30)];
        _countTextFeild.delegate = self;
        _countTextFeild.font = [UIFont systemFontOfSize:15];
        _countTextFeild.borderStyle = UITextBorderStyleRoundedRect;
        _countTextFeild.placeholder = @"轰炸次数";
        _countTextFeild.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _countTextFeild;
}

- (UITextField *)timeTextFeild
{
    if(!_timeTextFeild){
        _timeTextFeild = [[UITextField alloc] initWithFrame:CGRectMake(120, 370, 165, 30)];
        _timeTextFeild.delegate = self;
        _timeTextFeild.font = [UIFont systemFontOfSize:15];
        _timeTextFeild.borderStyle = UITextBorderStyleRoundedRect;
        _timeTextFeild.placeholder = @"时间间隔(秒)";
        _timeTextFeild.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _timeTextFeild;
}

//轰炸间隔
- (UISlider *)slider1
{
    if (!_slider1)
    {
        _slider1 = [[UISlider alloc] initWithFrame:CGRectMake(110, 45, 150, 30)];
        _slider1.minimumValue = 0;
        _slider1.maximumValue = 100;
        _slider1.value = 1;
        _slider1.continuous = YES;
        [self.bgView2 addSubview:_slider1];
        [_slider1 addTarget:self action:@selector(sliderValueChanged1:) forControlEvents:UIControlEventValueChanged];  
    }
    return _slider1;
}

//轰炸次数
- (UISlider *)slider2
{
    if (!_slider2)
    {
        _slider2 = [[UISlider alloc] initWithFrame:CGRectMake(110, 85, 150, 30)];
        _slider2.minimumValue = 0;
        _slider2.maximumValue = 100;
        _slider2.value = 0;
        _slider2.continuous = YES;
        [self.bgView2 addSubview:_slider2];
        [_slider2 addTarget:self action:@selector(sliderValueChanged2:) forControlEvents:UIControlEventValueChanged];  

    }
    return _slider2;
}

//自定义编号
- (UISlider *)slider3
{
    if (!_slider3)
    {
        _slider3 = [[UISlider alloc] initWithFrame:CGRectMake(90, 45, 170, 30)];
        _slider3.minimumValue = 1;
        _slider3.maximumValue = 99;
        _slider3.value = 1;
        _slider3.continuous = YES;
        [self.bgView3 addSubview:_slider3];
        [_slider3 addTarget:self action:@selector(sliderValueChanged3:) forControlEvents:UIControlEventValueChanged];  

    }
    return _slider3;
}

- (void)sliderValueChanged1:(UISlider *)slider
{
    _labNum1.text = [NSString stringWithFormat:@"%.0f",slider.value];
}

- (void)sliderValueChanged2:(UISlider *)slider
{
    _labNum2.text = [NSString stringWithFormat:@"%.0f",slider.value];
}

- (void)sliderValueChanged3:(UISlider *)slider
{
    _labNum3.text = [NSString stringWithFormat:@"%.0f",slider.value];
}
- (UILabel *)labNum1
{
    if (!_labNum1)
    {
        _labNum1 = [[UILabel alloc] initWithFrame:CGRectMake(265, 45, 50, 30)];
        _labNum1.font = [UIFont systemFontOfSize:13];
        _labNum1.text = @"0";
        [self.bgView2 addSubview:_labNum1];
    }
    return _labNum1;
}

- (UILabel *)labNum2
{
    if (!_labNum2)
    {
        _labNum2 = [[UILabel alloc] initWithFrame:CGRectMake(265, 85, 50, 30)];
        _labNum2.font = [UIFont systemFontOfSize:13];
        _labNum2.text = @"0";
        [self.bgView2 addSubview:_labNum2];
    }
    return _labNum2;
}

- (UILabel *)labNum3
{
    if (!_labNum3)
    {
        _labNum3 = [[UILabel alloc] initWithFrame:CGRectMake(265, 45, 50, 30)];
        _labNum3.font = [UIFont systemFontOfSize:13];
        _labNum3.text = @"1";
        [self.bgView3 addSubview:_labNum3];
    }
    return _labNum2;
}

- (UIView *)line1
{
    if (!_line1)
    {
        _line1 = [[UIView alloc] initWithFrame:CGRectMake(10, 405, self.view.frame.size.width-20, 0.5)];
        _line1.alpha = 0.5;
        _line1.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:_line1];
    }
    return _line1;
}

- (UIView *)line2
{
    if (!_line2)
    {
        _line2 = [[UIView alloc] initWithFrame:CGRectMake(10, 435, self.view.frame.size.width-20, 0.5)];
        _line2.alpha = 0.5;
        _line2.backgroundColor = [UIColor lightGrayColor];
        [self.scrollView addSubview:_line2];
    }
    return _line2;
}

- (HCustomNumberView *)customNumberView {
    if (!_customNumberView) {
        _customNumberView = [[HCustomNumberView alloc] initWithFrame:CGRectMake(10, 30, SCREEN_WIDTH - 20, SCREEN_WIDTH + 50)];
        _customNumberView.BtnBlock = ^(UIButton *button) {
            [self actionBtnCLickOnCustomNumberView:button];
        };
    }
    return _customNumberView;
}


- (NSMutableArray *)customNumberArray {
    if (!_customNumberArray) {
        _customNumberArray = HCustomNumberArray().mutableCopy;
        if (!_customNumberArray) {
            _customNumberArray = [[NSMutableArray alloc] init];
        }
    }
    return _customNumberArray;
}

- (NSMutableArray *)customServiceArray {
    if (!_customServiceArray) {
        _customServiceArray = HServiceTextArray().mutableCopy;
        if (!_customServiceArray) {
            _customServiceArray = [[NSMutableArray alloc] init];
        }
    }
    return _customServiceArray;
}
#pragma mark - UITextViewDelegate

#pragma mark - array  按顺序排列

/// 数组排序 按内部字典的key 从小-大排序
- (NSMutableArray *)sortWith:(NSArray *)array
{
    if (array.count == 1) {
        return array.mutableCopy;
    }
    array = [array sortedArrayUsingComparator:^NSComparisonResult(NSMutableDictionary *dict1, NSMutableDictionary *dict2) {
        return [dict1.allKeys.firstObject compare:dict2.allKeys.firstObject];
    }];
    return array.mutableCopy;
}

#pragma mark - 隐藏键盘

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGPoint point = [textField convertPoint:CGPointMake(0, 0) toView:self.view];
    
    float height = textField.height+point.y+300; //256键盘高度，42中文联想提示框高度
    if (height > SCREEN_HEIGHT) {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.frame = CGRectMake(0, SCREEN_HEIGHT-height+textField.height, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
    }
    
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    CGPoint point = [textView convertPoint:CGPointMake(0, 0) toView:self.view];
    
    float height = textView.height+point.y+256; //258键盘高度，42中文联想提示框高度

    if (height > SCREEN_HEIGHT) {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.frame = CGRectMake(0, SCREEN_HEIGHT-100-height+textView.height, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
    }
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];
    return YES;
}

- (void)hiddenKeyboard
{
    [UIView animateWithDuration:0.3 animations:^{
        [self.view endEditing:YES];
        self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    }];

}
@end
